#include "fig_ui_shop.h"
#include "fig_ui_generalinfo.h"
#include "fig_vars.h"

#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "ui/ui_slidebox.h"
#include "lang/lang.h"

namespace ui
{
 class UISlideBox;
}


const int FIGUIShop::ITEM_PRICES[] = { 0, 1240, 6540  };

FIGUIShop::FIGUIShop(camera::FbCamera*	pCamera) :
				FIGMOGAUIState(pCamera, "ui/shop.ui"),
				m_pSlideBox(NULL)
{
	
	SetupTransition("button_back", "level_select");
	//SetupTransition("button_select", "comic_menu");


	// Place Shop items properly 
	char Buffer[256];
	
	math::Vec2 TempPos = math::Vec2(0.5f,0.35f);
	for(int i = 0 ; i < MAX_SHOP_ITEM; i++)
	{		
		sprintf(Buffer, "item_%i", i+1);		
	
		GetWidget(Buffer)->SetPositionRelativeToParent(math::Vec2(TempPos.x, GetWidget(Buffer)->GetPosition().y) );		
		TempPos.x += GetWidget(Buffer)->GetSize().x;
	}


	// set up sliding frame 
	ui::UIWidget* pTempWidget = NULL;
	pTempWidget  = GetWidget("DragBox");
	const int CURRENT_ITEM = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_SELECT_AIRCRAFT);
	m_pSlideBox = new ui::UISlideBox(ui::UISlideBox::SLIDE_DIR_HOR, 
													pTempWidget, 
													MAX_SHOP_ITEM-1, 
													CURRENT_ITEM, 
													GetWidget("item_1")->GetSize().x,	
													pTempWidget->GetPosition(),
													pTempWidget->GetPosition() + pTempWidget->GetSize()
													);

	m_pSlideBox->SetChangeCallBack( UI_DELEGATE(FIGUIShop, OnSlideChange));
	
	m_pUIButtonPrice = GetWidget("button_price");
	m_pUITxtPrice = GetWidget("txt_price");
	m_pUIButtonSelect = GetWidget("button_select");
	m_pUIGameMoney = GetWidget("game_money");

	SetWidgetFunctor("button_price",UI_DELEGATE(FIGUIShop, OnButtonPrice));	
	SetWidgetFunctor("button_select",UI_DELEGATE(FIGUIShop, OnButtonSelect));	
	

	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_STARS_COLLECTED,10000);
	const int CurrentGameMoney =  FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_STARS_COLLECTED) - FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_STARS_SPEND);
		
	if(m_pUIGameMoney)
		m_pUIGameMoney->SetTextA( lang::Lang::FormatNumber(CurrentGameMoney,true) );


	m_pUITxtPrice->SetTextColor(1.0f,0.1f,0.1f,0.9f);
	for(int i = 0; i < MAX_SHOP_ITEM; i++)
	{
		sprintf(Buffer, "selected_%i", i+1);			
		m_pUISelectedShipGlow[i] = GetWidget(Buffer);
	}

	// call last
	SlideChange();


	

}
	

FIGUIShop::~FIGUIShop()
{
	UTIL_SAFE_DELETE(m_pSlideBox);
}

bool		FIGUIShop::Tick(float dt)
{
	m_pSlideBox->Tick(dt);
	return FIGMOGAUIState::Tick(dt);
}


void		FIGUIShop::MouseButtonReleased(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos)
{
	if(pKey->ButtonID == input::Mouse::BUTTON_LEFT)
		m_pSlideBox->MouseButtonReleased(Pos);
}

void		FIGUIShop::MouseButtonDown(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos)
{
	if( gameflow::UIPopupManager::Instance().GetActivePopup())
		return;

	if(pKey->ButtonID == input::Mouse::BUTTON_LEFT)		
		m_pSlideBox->MouseButtonDown(Pos);
}

void DELEGATE_CALL	FIGUIShop::OnButtonPrice(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	// buying a new item
	

	int Currnet_Item = m_pSlideBox->GetCurrentVal();
	const int CurrentGameMoney = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_STARS_SPEND) - FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_STARS_SPEND);
	if( CurrentGameMoney < ITEM_PRICES[Currnet_Item])
	{
		// not enough money
		gameflow::UIPopup* pPopup = gameflow::UIPopupManager::Instance().StartPopup("ui/general_info.ui");
		//if(pPopup)
		//	pPopup->ShowThis("not enough game money",0.0f);
	}
	else
	{
		// need to reduce game_money accordingly
		// need to save

		int MoneySpend = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_STARS_SPEND);
		MoneySpend += ITEM_PRICES[Currnet_Item];
		if( MoneySpend > FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_STARS_COLLECTED))
		{
			// shouldn't be
		}
		else
		{
			FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_STARS_SPEND, MoneySpend);			
			FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_BOUGHT_START + Currnet_Item , 1);
			m_pUIButtonSelect->SetIsVisible(true);
			m_pUIButtonPrice->SetIsVisible(false);	

			FIGVars::Instance()->Save();
		}

		
	}

}


void DELEGATE_CALL  FIGUIShop::OnSlideChange(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	SlideChange();
	
}

void	FIGUIShop::SlideChange()
{
	// check whether it's already bought or not
	//if bought, show , button_select
	//if not bought , show button_price

	int Currnet_Item = m_pSlideBox->GetCurrentVal();
	

	if(Currnet_Item == 0)
	{
		m_pUIButtonSelect->SetIsVisible(true);
		m_pUIButtonPrice->SetIsVisible(false);	
	}
	else
	{
		
		const int IsBought = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_BOUGHT_START + Currnet_Item);		

		if(IsBought > 0)
		{
			m_pUIButtonSelect->SetIsVisible(true);
			m_pUIButtonPrice->SetIsVisible(false);	
		}
		else
		{
			// if not bought
			m_pUIButtonSelect->SetIsVisible(false);
			m_pUIButtonPrice->SetIsVisible(true);

			m_pUITxtPrice->SetTextA( lang::Lang::FormatNumber( ITEM_PRICES[Currnet_Item], true));
		}
		
	}


	for(int i = 0; i < MAX_SHOP_ITEM; i++)
		m_pUISelectedShipGlow[i]->SetIsVisible(false);	

	m_pUISelectedShipGlow[Currnet_Item]->SetIsVisible(true);

}

void DELEGATE_CALL		FIGUIShop::OnButtonSelect(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	int Currnet_Item = m_pSlideBox->GetCurrentVal();
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_SELECT_AIRCRAFT, Currnet_Item);
	gameflow::GameFlowManager::Instance().SetTransitionEffect("fade","comic_menu","");
	
}


